package com.jinzhi.jzapi.training.task;

import com.jinzhi.jzweb.domain.OrderDO;
import com.jinzhi.jzweb.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Slf4j
@Component
public class AutoTraEffectiveTimesJob implements Job {

    @Autowired
    private OrderService orderService;

    @Override
    @Transactional
    public void execute(JobExecutionContext jobExecutionContext){
        log.info("---------------------开始进行课程有效期计算---------------------");
        //查询所有已付款的课程订单
        List<OrderDO> orderDOS = orderService.findByKv("type",0,"state",0);
        if(orderDOS != null && orderDOS.size() > 0){
            for(OrderDO orderDO : orderDOS){
                Calendar c = Calendar.getInstance();
                c.setTime(orderDO.getPayTime());
                c.add(Calendar.MONTH, (int) (orderDO.getEffectiveTime() * 12));
                Date m3 = c.getTime();
                Date date = new Date();
                if(date.after(m3)){//当现在时间大于课程有效期时，将订单改为已过期
                    orderDO.setState(3);
                    orderService.updateById(orderDO);
                }
            }
        }
    }

}
